package com.jnu.ilearn.utils;


import android.widget.Toast;

import com.jnu.ilearn.bean.User;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Properties;

public class DBUtils {

    public static Connection connect(){
//        //1、加载配置文件
//        InputStream inputStream = DBUtils.class.getClassLoader().getResourceAsStream("DBproperties.properties");
//        Properties properties = new Properties();
//        try {
//            if(inputStream != null){
//                properties.load(inputStream);
//            }else{
//                return null;
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//
//        //2、读取配置文件
//        String user = "root";
//        String password = "19_Learn";
//        String url = "jdbc:mysql://120.24.40.131:3306/iLearn";
//        String driverClass = "com.mysql.jdbc.Driver";
//
//        //3、加载驱动
//        try {
//            Class.forName(driverClass);
//        } catch (ClassNotFoundException e) {
//            e.printStackTrace();
//        }
//
//        //4、获取连接
//        try {
//            Connection conn = DriverManager.getConnection(url,user,password);
//            return conn;
//        } catch (SQLException throwables) {
//            throwables.printStackTrace();
//        }

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException cne) {
            cne.printStackTrace();
        }
        String dburl = "jdbc:mysql://120.24.40.131:3306/iLearn?&useSSL=false&serverTimezone=UTC";
        String sql = "SELECT * FROM users where id < 5";
        Connection conn ;
        try{
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Class.forName("com.mysql.jdbc.Driver");
                        String dburl = "jdbc:mysql://120.24.40.131:3306/iLearn?&useSSL=false&serverTimezone=UTC";
                        String sql = "SELECT * FROM user";
                        Connection conn = DriverManager.getConnection(dburl, "root", "19_Learn");
                        Statement stmt = conn.createStatement();
                        ResultSet rst = stmt.executeQuery(sql);
                        while (rst.next()){
                            int id=rst.getInt(1);
                            String string=rst.getString(3);
                            String axxis=rst.getString(4);
                        }

                        disconnect(conn);
                    }
                    catch (Exception e) {
                        e.printStackTrace();
                    }

                }
            }).start();

        }catch (Exception e){
            e.printStackTrace();
        }

        return null;
    }

    public static boolean disconnect(Connection connection){
        try {
            if(connection!=null){
                connection.close();
                return true;
            }else{
                return false;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return false;
        }
    }
}
